<?php
namespace Home\Controller;
use Think\Controller;
class CustomerController extends ZMController {
	public function customer(){
	    $custname=I('custname');
		$region=session('user.region');
		$like='';
		if(!empty($custname)){
			$like.="and (a.name like '%$custname%' or a.othername like '%$custname%')";
		}
		else{
			$like.="";
		}
		if($region!=''&&$region!=999){
			$like.=" and a.region=$region";
		}
		else if($region==''){
			exit(0);
		}
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$hospital=R('Cust/hospital');//延伸系统医院
		$hospital=i_array_column($hospital,'hid');
		$hospital=implode(',',$hospital);
		$Model=M()->db(2,'DB_CONFIG2');
		$count = $Model->query("select a.id from ddwk_customer as a where 1=1 $like and a.id in ($hospital)");
		$res=$Model->query("select a.id,a.name,a.address,a.linkman,a.linktel from ddwk_customer as a where 1=1 $like and a.id in ($hospital) $limit");
		foreach($res as &$row){
			$row['booked']=1;
		}
		$count=count($count);
		$this->ajax($res,$count);
    }
	public function selectEx(){//物业公司
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_area where hid=$hid and station=1 and isdel=0");
		$res=$Model->query("select areaname,memo from ddwk_area where hid=$hid and station=1 and isdel=0 order by id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectArea(){//医护部门
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_area where hid=$hid and station=2 and isdel=0");
		$res=$Model->query("select a.areaname,a.type,a.memo,b.name as point from ddwk_area as a left join ddwk_hcustomer as b on a.hcust=b.id where a.hid=$hid and a.station=2 and a.isdel=0 order by a.id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectS(){//收集人员
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_employee where hid=$hid and station=1 and isdel=0");
		$res=$Model->query("select b.areaname,a.id as nfc1,a.name,a.memo,c.nfcname as nfc2 from ddwk_employee as a left join ddwk_area as b on a.area=b.id left join ddwk_nfc1 as c on a.nfcid=c.id where a.hid=$hid and a.station=1 and a.isdel=0 order by a.id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectE(){//审核人员
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_employee where hid=$hid and station=2 and isdel=0");
		$res=$Model->query("select b.areaname,a.id as nfc1,a.name,a.memo,c.nfcname as nfc2 from ddwk_employee as a left join ddwk_area as b on a.area=b.id left join ddwk_nfc1 as c on a.nfcid=c.id where a.hid=$hid and a.station=2 and a.isdel=0 order by a.id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectH(){//产废点
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_hcustomer where hid=$hid and isdel=0");
		$res=$Model->query("select a.id,a.name,a.address,b.nfcname,a.areaid,a.memo from ddwk_hcustomer as a left join ddwk_nfc2 as b on a.nfcid=b.id where a.hid=$hid and a.isdel=0 order by a.id $limit");
		foreach($res as &$row){
			$hcid=$row['id'];
			$areaid=$row['areaid'];
			$r1=$Model->query("select b.name from ddwk_hcustomer_master as a left join ddwk_employee as b on a.employeeid=b.id where a.hcid=$hcid");
			$desc = '';
            foreach($r1 as $row1){
                $desc.=','.$row1['name'];
            }
			$row['desc']=ltrim($desc,',');
			$areaid=explode(',',$areaid);
			$areadesc='';
			foreach($areaid as $row2){
				$r2=$Model->query("select areaname from ddwk_area where id=$row2 and isdel=0");
				$areadesc.=','.$r2['0']['areaname'];
			}
			$row['areadesc']=ltrim($areadesc,',');
		}
		$count=count($count);
		$this->ajax($res,$count);
	}
}